package cliente.vista;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;

import cliente.controle.ContaControlador;
import cliente.controle.ExtratoControlador;
import cliente.controle.Sistema;
import cliente.modelo.Conta;

public class VisualizarExtratoView {

	private JFrame tela;
	private DefaultTableModel tableModel;
	private JTable tabela;
	private JScrollPane scrollPane;
	private acao ouvinte;
	private JPanel painel;
	private JButton voltar;
	private JLabel saldoLabel;
	private JComboBox contas;

	public void iniciaVisualizarExtratoView() throws NumberFormatException,
			IOException {
		Sistema sistema = Sistema.getUnicaInstancia();

		ouvinte = new acao();
		tela = new JFrame();
		tela.setBounds(500, 100, 500, 600);
		tela.setResizable(false);
		tela.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		tela.setTitle("Visualizar Extrato");

		ArrayList<Conta> contasCorrentes = ContaControlador.getAll(sistema
				.getAtualId());
		contas = new JComboBox(contasCorrentes.toArray());
		contas.setSelectedIndex(0);
		contas.addActionListener(ouvinte);

		Conta conta = (Conta) contas.getSelectedItem();

		tableModel = new DefaultTableModel();

		// Adiciona nomes de colunas
		tableModel.addColumn("Data");
		tableModel.addColumn("Operacao");
		tableModel.addColumn("Movimentacao");

		saldoLabel = new JLabel();

		ExtratoControlador.carregaTabelaValues(conta.getId(), tableModel,
				saldoLabel);

		tabela = new JTable(tableModel);
		scrollPane = new JScrollPane(tabela);

		painel = new JPanel();
		GroupLayout layout = new GroupLayout(painel);
		painel.setLayout(layout);

		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		voltar = new JButton("Voltar");
		voltar.addActionListener(ouvinte);

		organizaFields(layout);

		tela.setLayout(new BorderLayout());
		tela.add(painel, BorderLayout.CENTER);
		tela.setVisible(true);

	}

	class acao implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (arg0.getSource() == voltar) {
				tela.dispose();
				try {
					new ClienteView().iniciaTela();
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (arg0.getSource() == contas) {
				Conta conta = (Conta) contas.getSelectedItem();
				ExtratoControlador.carregaTabelaValues(conta.getId(),
						tableModel, saldoLabel);
			}
		}
	}

	private void criaGruposVerticais(GroupLayout layout) {
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(contas));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(scrollPane));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(saldoLabel));
		vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(voltar));
		layout.setVerticalGroup(vGroup);
	}

	private void criaGruposHorizontais(GroupLayout layout) {
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup(Alignment.CENTER)
				.addComponent(contas).addComponent(scrollPane).addComponent(
						saldoLabel).addComponent(voltar));
		layout.setHorizontalGroup(hGroup);
	}

	private void organizaFields(GroupLayout layout) {
		criaGruposHorizontais(layout);
		criaGruposVerticais(layout);
	}

}
